package com.proyectofinal.ramas.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.proyectofinal.ramas.domain.User;
import com.proyectofinal.ramas.domain.UserRole;
import com.proyectofinal.ramas.domain.UserRolePK;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service for processing UserRoles
 * 
 */
@Service("userRoleService")
@Transactional
public class UserRoleService {

	protected static Logger logger = Logger.getLogger("service");
	
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	/**
	 * Retrieves all userRoles
	 * 
	 * @return a list of userRoles
	 */
	public List<UserRole> getAll() {
		logger.debug("Retrieving all userRoles");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM  UserRole");
		
		// Retrieve all
		return  query.list();
	}
        
        /**
	 * @return a list of userRoles by user
	 */
	public List<UserRole> getAll(User user) {
		logger.debug("Retrieving all userRoles from user "+user.getUserName());
		String userID = user.getUserID().toString();
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM  UserRole WHERE userID = :userID");
		query.setParameter("userID", userID);
		// Retrieve all
		return  query.list();
	}
	
	/**
	 * Retrieves a single userRole
	 */
	public UserRole get( UserRolePK userRoleID ) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Retrieve existing UserRole first
		UserRole userRole = (UserRole) session.get(UserRole.class, userRoleID);
		
		return userRole;
	}
	/**
	 * Adds a new userRole
	 */
	public void add(UserRole userRole) {
		logger.debug("Adding new role");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Save
		session.save(userRole);
	}
	
	/**
	 * Deletes an existing userRole
	 * @param userRoleID the userRoleID of the existing userRole
	 */
	public void delete(UserRolePK userRoleID) {
		logger.debug("Deleting existing role");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Retrieve existing userRole first
		UserRole userRole = (UserRole) session.get(UserRole.class, userRoleID);
		
		// Delete 
		session.delete(userRole);
	}
	
	/**
	 * Edits an existing userRole
	 */
	public void edit(UserRole userRole) {
		logger.debug("Editing existing role");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Retrieve existing role via userRoleID
		UserRole existingUserRole = (UserRole) session.get(UserRole.class, userRole.getUserRolePK());
		
		// Assign updated values to this userRole
		existingUserRole.setUserRoleStatus(userRole.getUserRoleStatus());
		

		// Save updates
		session.save(existingUserRole);
	}
}
